#!/usr/bin/env python
# -*- coding: utf-8 -*-

from threading import Thread,Lock

g_num = 0
mutex = Lock()
def incr():
    global g_num
    for i in range(1000000):
        if mutex.acquire():
            g_num += 1
            mutex.release()
    print("result=%d"%g_num)

def incr2():
    global g_num
    for i in range(1000000):
        with mutex:
            g_num += 1
    print("result=%d"%g_num)

t1 = Thread(target=incr2)
t2 = Thread(target=incr2)

t1.start()
t2.start()